<?php

$stime = microtime(true);

include "ip.php";
include "useragent.php";

$ip = "";
$useragent = "";
$content = "";
$num = 0;
$format = $_REQUEST["format"];

for ($i = 0; $i < 3; $i++) {
    $num++;
    $ip = $ips[rand(0, (count($ips) - 1))];
    $useragent = $useragents[rand(0, (count($useragents) - 1))];
    $result = getHtml($_REQUEST["term"], $_REQUEST["filter"], $_REQUEST["sort"],$_REQUEST["sort_order"], $_REQUEST["page"], $_REQUEST["format"], $_REQUEST["size"], $ip, $useragent, "");
    $content = $result["body"];

    if ($content != "" && !strpos($content, "The document has moved")) {

        break;
    } else {
        @file_put_contents("result.txt", $ip . "\r\n", FILE_APPEND);
    }
}


$result = array();
//if (strpos($content, "Found 1 result for")) { 原始备份 现在详情页不带 Found 1 result for 了
if (!strpos($content, "log_resultcount")) {
    $list = array();
    $info = array();
    $html = str_replace("\r", "", $content);
    $html = str_replace("\n", "", $content);
    $html = preg_replace("/\s(?=\s)/", "\\1", $content);
    $result["type"] = 1;
    $result["num"] = 1;
    $result["term"] = $_REQUEST["term"];
    //token
    preg_match("/<input type=\"hidden\" name=\"csrfmiddlewaretoken\" value=\"(.+?)\">/", $html, $match);
    if ($match) $result["token"] = trim($match[1]);
    else $result["token"] = "";
    //Summary  title  pmid intro source
    preg_match("/<h1 class=\"heading-title\">(.+?)<\\/h1>/is", $html, $match);
    if ($match) $info["title"] = trim($match[1]);
    else $info["title"] = "";

    preg_match("/<strong class=\"current-id\" title=\"PubMed ID\">(.+?)<\\/strong>/", $html, $match);
    if ($match) $info["pmid"] = $match[1];
    else $info["pmid"] = "";

    // 添加撤稿文章 单个文章模式
    preg_match("/<h3 class=\"usa-alert-heading\">Retracted article<\\/h3>/", $html, $match);
    if ($match) $info["Retracted"] = "1";
    else $info["Retracted"] = "0";


    preg_match("/id=\"en-abstract\"> <p>(.+?)<\\/p>/", $html, $match);
    if ($match) $info["intro"] = substr(preg_replace("/<.+?>/is", "", $match[1]), 0, 300) . "...";
    else $info["intro"] = "";


    //Abstract
    preg_match("/<div class=\"publication-type\" >(.+?)<\\/div>/", $html, $match);
    if ($match) $info["ptype"] = $match[1];
    else $info["ptype"] = "";

    preg_match("/<div class=\"journal-actions dropdown-block\"><button .+?> (.+?) <\\/button>/", $html, $match);
    if ($match) $info["jour"] = $match[1];
    else $info["jour"] = "";

    preg_match("/<span class=\"volume-issue-pages\"> (.+?) <\\/span>/", $html, $match);
    if ($match) $info["vip"] = $match[1];
    else $info["vip"] = "";

    preg_match("/<span class=\"publication-date\" >(.+?)<\\/span>/", $html, $match);
    if ($match) $info["year"] = preg_replace("/<.+?>/is", "", $match[1]);
    else $info["year"] = "";

    $source = "";
    preg_match("/<div class=\"authors-list\">(.+?)<\\/div>/", $html, $match);
    if ($match) {
        $aushtml = preg_replace("/<span class=\"author-sup-separator\">.+?<\\/span>/", "", $match[1]);
        preg_match_all("/<span class=\"authors-list-item \">(.+?)<\\/span>/", $aushtml, $matchess);
        $aus = array();
        for ($j = 0; $j < count($matchess[1]); $j++) {
            $au = array();
            $auhtml = $matchess[1][$j];
            preg_match("/<a class=\"full-name\" href=.+?erm=(.+?)&amp;cauthor_id.+?data-ga-action=\"author_link\".+?>(.+?)<\\/a>/", $auhtml, $matchau);
            if ($matchau) {
                $au["nameiso"] = str_replace("+", " ", $matchau[1]);
                $au["name"] = $matchau[2];
                preg_match_all("/<a class=\"affiliation-link\".+?>(.+?)<\\/a>/", $auhtml, $matchaunum);
                if ($matchaunum) {
                    for ($a = 0; $a < count($matchaunum[1]); $a++) {
                        $au["num"][$a] = $matchaunum[1][$a];
                    }
                } else {
                    $au["num"] = array();
                }
                $aus[$j] = $au;
            }
        }
        $info["author"] = $aus;
    } else {
        $info["author"] = array();
    }

    preg_match("/<span class=\"docsum-authors full-authors\">(.+?)<\\/span>/", $html, $match);
    if ($match) {
        $info["authorls"] = $match[1];
    } else {
        $info["authorls"] = "";
    }


    preg_match_all("/<li data-affiliation-id=\"affiliation.+?>(.+?)<\\/li>/", $html, $match);
    if ($match) {
        $affs = array();
        for ($j = 0; $j < count($match[1]) / 2; $j++) {
            $af = array();
            $affhtml = $match[1][$j];
            preg_match("/<sup class=\"key\">(.+?)<\\/sup> (.+)/", $affhtml, $match1);
            if ($match1) {
                $af["num"] = $match1[1];
                $af["name"] = $match1[2];
                $affs[$j] = $af;
            }
        }
        $info["affs"] = $affs;
    } else {
        $info["affs"] = array();
    }
    preg_match("/<span class=\"id-label\"> PMCID: <\\/span> <a .+?> (.+?) <\\/a>/", $html, $match);
    if ($match) $info["pmcid"] = $match[1];
    else $info["pmcid"] = "";

    preg_match("/<span class=\"id-label\"> DOI: <\\/span> <a .+?> (.+?) <\\/a>/", $html, $match);
    if ($match) $info["doi"] = $match[1];
    else $info["doi"] = "";

    preg_match("/id=\"en-abstract\">(.+?)<\\/div>/", $html, $match);
    if ($match) $info["abstr"] = trim($match[1]);
    else $info["abstr"] = "";

    preg_match("/<p> <strong class=\"sub-title\"> Keywords: <\\/strong>.+?<\\/p>/", $html, $match);
    if ($match) $info["keyword"] = $match[0];
    else $info["keyword"] = "";

    preg_match("/id=\"copyright\"> (.+?) <\\/p>/", $html, $match);
    if ($match) $info["copyright"] = $match[1];
    else $info["copyright"] = "";


    //Publication types  MeSH terms  Substances  Associated data  Grant support

    //Publication types
    preg_match("/<h2 class=\"title\"> Publication types <\\/h2>(.+?)<\\/ul> <\\/div>/", $html, $matchpt);
    if ($matchpt) {
        $pts = array();
        preg_match_all("/<li>(.+?)<\\/div><\\/li>/", $matchpt[1], $pubsHtml);
        if ($pubsHtml) {
            for ($j = 0; $j < count($pubsHtml[1]); $j++) {
                $pt = array();
                preg_match("/<button class=\"keyword-actions-trigger trigger keyword-link.+?>(.+?)<\\/button>.+?<ul class=\"keyword-actions-links\">(.+?)<\\/ul>/", $pubsHtml[1][$j], $ptss1);
                if ($ptss1) {
                    $pt["type"] = 2;
                    $pt["title"] = trim($ptss1[1]);
                    preg_match_all("/<a .+? href=\"(.+?)\".+?>(.+?)<\\/a>/", $ptss1[2], $ptss2);
                    if ($ptss2) {
                        for ($m = 0; $m < count($ptss2[1]); $m++) {
                            $pt["hrefs"][$m]["name"] = trim($ptss2[2][$m]);
                            $pt["hrefs"][$m]["href"] = $ptss2[1][$m];
                        }
                    }
                    preg_match("/<a class=\"add-to-search-link .+? data-search-term=\"(.+?)\" .+?> (.+?) <\\/a>/", $ptss1[2], $ptss3);
                    if ($ptss3) {
                        $pt["hrefs"][count($ptss2[1])]["name"] = $ptss3[2];
                        $pt["hrefs"][count($ptss2[1])]["href"] = str_replace("&quot;", "", $ptss3[1]);
                    }
                }
                $pts[$j] = $pt;
            }
        }
        $info["pts"] = $pts;
    } else {
        $info["pts"] = array();
    }

    //MeSH terms
    preg_match("/<h2 class=\"title\"> MeSH terms <\\/h2>(.+?)<\\/ul> <\\/div>/", $html, $matchMesh);
    if ($matchMesh) {
        $kws = array();
        preg_match_all("/<li>(.+?)<\\/div><\\/li>/", $matchMesh[1], $meshHtml);
        if ($meshHtml) {
            for ($j = 0; $j < count($meshHtml[1]); $j++) {
                $kw = array();
                preg_match("/<button class=\"keyword-actions-trigger trigger keyword-link.+?>(.+?)<\\/button>.+?<ul class=\"keyword-actions-links\">(.+?)<\\/ul>/", $meshHtml[1][$j], $kwss1);
                if ($kwss1) {
                    $kw["type"] = 2;
                    if (strpos($kwss1[1], "asterisk")) {
                        preg_match("/(.+?) <span class=\"asterisk\">/", $kwss1[1], $matchkw);
                        if ($matchkw) {
                            $kw["title"] = trim($matchkw[1]) . "*";
                        }
                    } else {
                        $kw["title"] = trim($kwss1[1]);
                    }

                    preg_match_all("/<a .+? href=\"(.+?)\".+?>(.+?)<\\/a>/", $kwss1[2], $kwss2);
                    if ($kwss2) {
                        for ($m = 0; $m < count($kwss2[1]); $m++) {
                            $kw["hrefs"][$m]["name"] = trim($kwss2[2][$m]);
                            $kw["hrefs"][$m]["href"] = $kwss2[1][$m];
                        }
                    }
                    preg_match("/<a class=\"add-to-search-link .+? data-search-term=\"(.+?)\" .+?> (.+?) <\\/a>/", $kwss1[2], $kwss3);
                    if ($kwss3) {
                        $kw["hrefs"][count($kwss2[1])]["name"] = $kwss3[2];
                        $kw["hrefs"][count($kwss2[1])]["href"] = str_replace("&quot;", "", $kwss3[1]);
                    }
                }
                $kws[$j] = $kw;
            }
        }
        $info["meshs"] = $kws;
    } else {
        $info["meshs"] = array();
    }
    //Substances
    preg_match("/<h2 class=\"title\"> Substances <\\/h2>(.+?)<\\/ul> <\\/div>/", $html, $meshSub);
    if ($meshSub) {
        $subs = array();
        preg_match_all("/<li>(.+?)<\\/div><\\/li>/", $meshSub[1], $subHtml);
        if ($subHtml) {
            for ($j = 0; $j < count($subHtml[1]); $j++) {
                $sub = array();
                preg_match("/<button class=\"keyword-actions-trigger trigger keyword-link.+?>(.+?)<\\/button>.+?<ul class=\"keyword-actions-links\">(.+?)<\\/ul>/", $subHtml[1][$j], $subss1);
                if ($subss1) {
                    $sub["type"] = 2;
                    $sub["title"] = trim($subss1[1]);
                    preg_match_all("/<a .+? href=\"(.+?)\".+?>(.+?)<\\/a>/", $subss1[2], $subss2);
                    if ($subss2) {
                        for ($m = 0; $m < count($subss2[1]); $m++) {
                            $sub["hrefs"][$m]["name"] = trim($subss2[2][$m]);
                            $sub["hrefs"][$m]["href"] = $subss2[1][$m];
                        }
                    }
                    preg_match("/<a class=\"add-to-search-link .+? data-search-term=\"(.+?)\" .+?> (.+?) <\\/a>/", $subss1[2], $subss3);
                    if ($subss3) {
                        $sub["hrefs"][count($subss2[1])]["name"] = $subss3[2];
                        $sub["hrefs"][count($subss2[1])]["href"] = str_replace("&quot;", "", $subss3[1]);
                    }
                }
                $subs[$j] = $sub;
            }
        }
        $info["subs"] = $subs;
    } else {
        $info["subs"] = array();
    }

    //Associated data
    preg_match("/<h3 class=\"title\"> Associated data <\\/h3>(.+?)<\\/ul> <\\/div>/", $html, $associated);
    if ($associated) {
        $assData = array();
        if (strpos($associated[1], "<div class=\"title\">Actions</div>")) {
            preg_match_all("/<li>(.+?)<\\/div><\\/li>/", $associated[1], $assDatas);
            if ($assDatas) {
                for ($j = 0; $j < count($assDatas[1]); $j++) {
                    $assd = array();
                    preg_match("/aria-describedby=\"supplemental-data.+?>(.+?)<\\/button>.+?<ul class=\"supplemental-data-actions-links\">(.+?)<\\/ul>/", $assDatas[1][$j], $ass1);
                    if ($ass1) {
                        $assd["type"] = 2;
                        $assd["title"] = trim($ass1[1]);
                        preg_match_all("/<a .+? title=\"(.+?)\" href=\"(.+?)\".+?>/", $ass1[2], $ass2);
                        if ($ass2) {
                            for ($m = 0; $m < count($ass2[1]); $m++) {
                                $assd["hrefs"][$m]["name"] = $ass2[1][$m];
                                $assd["hrefs"][$m]["href"] = $ass2[2][$m];
                            }
                        }
                    }
                    $assData[$j] = $assd;
                }
            }
        } else {
            preg_match_all("/<a .+? href=\"(.+?)\" .+?>(.+?)<\\/a>/", $associated[1], $assDatas2);
            if ($assDatas2) {
                for ($n = 0; $n < count($assDatas2[1]); $n++) {
                    $assData[$n]["type"] = 1;
                    $assData[$n]["title"] = trim($assDatas2[2][$n]);
                    $assData[$n]["href"] = $assDatas2[1][$n];
                }
            }
        }
        $info["assData"] = $assData;
    } else {
        $info["assData"] = array();
    }


    //Grant support

    preg_match("/<h3 class=\"title\"> Grant support <\\/h3>(.+?)<\\/ul><\\/div>/", $html, $supports);
    if ($supports) {
        $grants = array();
        if (strpos($supports[1], "<div class=\"title\">Actions</div>")) {
        } else {
            preg_match_all("/<a title=\"All articles for grant .+? href=\"(.+?)\" .+?> (.+?) <\\/a>/", $supports[1], $support);
            if ($support) {
                for ($n = 0; $n < count($support[1]); $n++) {
                    $grants[$n]["type"] = 1;
                    $grants[$n]["title"] = trim($support[2][$n]);
                    $grants[$n]["href"] = $support[1][$n];
                }
            }
        }
        $info["grants"] = $grants;
    } else {
        $info["grants"] = array();
    }

    //Grant support
    preg_match("/<div class=\"funding-statement\">(.+?)<\\/div>/", $html, $funding);
    if ($funding) {
        $grants = array();
        $grants[0]["type"] = 0;
        $grants[0]["title"] = $funding[1];
        $result["grants"] = $grants;
    }


    //full-text-links-list
    preg_match("/<div class=\"full-text-links-list\">(.+?)<\\/div>/is", $html, $matchfull);
    if ($matchfull) {
        $fulltext = array();
        preg_match_all("/<a class=\"link-item(.+?)<\\/a>/", $matchfull[1], $fulls);
        if ($fulls) {
            for ($j = 0; $j < count($fulls[0]); $j++) {
                $fullt = array();
                preg_match("/<a class=\"link-item.+?href=\"(.+?)\"/", $fulls[0][$j], $full1);
                if ($full1) {
                    $fullt["href"] = $full1[1];
                } else {
                    $fullt["href"] = "";
                }
                preg_match("/<img src=\"(.+?)\"/", $fulls[0][$j], $full2);
                if ($full2) {
                    $fullt["img"] = $full2[1];
                } else {
                    $fullt["img"] = "";
                }
                preg_match("/<span class=\"text\"> (.+?) <\\/span>/", $fulls[0][$j], $full3);
                if ($full3) {
                    $fullt["name"] = $full3[1];
                    if ($fullt["name"] == "Free PMC article") {
                        $fullt["img"] = "../img/pmcImg.png";
                    }
                } else {
                    $fullt["name"] = "";
                }
                $fulltext[$j] = $fullt;
            }
        }
        $info["fulltext"] = $fulltext;
    }

//    $info["source"]=$source." ".$info["jour"].$info["time"]." - "."<em>".$info["ptype"]."</em>.PMID ".$info["pmid"];
    //2021年6月2日10:50:44
    /*添加获取来源测试*/
    preg_match("/<div class=\"article-source\">(.+?)<h1 class=\"heading-title\">/is", $html, $match);
    if ($match) {
        $info["source"] = $match[1];
        $info["source"] = preg_replace("/<.+?>/", "", $info["source"]);
    } else $info["source"] = "";
//    $info["source"] = $source . " " . $info["jour"] . $info["time"] . " - " . "<em>" . $info["ptype"] . "</em>";
    $list[0] = $info;
    $result["list"] = $list;
} else if (strpos($content, "log_resultcount")) {
    //
    $result["type"] = 2;
    //token
    preg_match("/<input type=\"hidden\" name=\"csrfmiddlewaretoken\" value=\"(.+?)\">/", $content, $match);
    if ($match) $result["token"] = trim($match[1]);
    else $result["token"] = "";
    //检索数量
    preg_match("/<meta name=\"log_resultcount\" content=\"(.+?)\"/", $content, $match);
    if ($match) {
        $result["num"] = $match[1];
    } else {
        $result["num"] = 0;
    }
    $result["term"] = $_REQUEST["term"];
    $content = str_replace("\r", "", $content);
    $content = str_replace("\n", "", $content);
    $content = preg_replace("/\s(?=\s)/", "\\1", $content);
    //results by year
    preg_match("/<table id=\"timeline-table\".+?<tbody>(.+?)<\\/tbody>/", $content, $timeline);
    if ($timeline) {
        preg_match_all("/<tr>(.+?)<\\/tr>/is", $timeline[1], $trs);
        if ($trs) {
            $list = array();
            for ($i = 0; $i < count($trs[1]); $i++) {
                $tds = $trs[1][$i];
                $yearResults = array();
                preg_match("/<td> (.+?) <\\/td> <td> (.+?) <\\/td>/is", $tds, $td);
                $yearResults["year"] = $td[1];
                $yearResults["count"] = $td[2];
                $list[$i] = $yearResults;
            }
            $result["yearResults"] = $list;
        }
    }


    $list = array();
    // 存放最匹配那个文章集合
    $list2 = array();
    //abstract模式
    if ($format == "abstract") {

        preg_match_all("/<article class=\"article-overview\">(.+?)<\\/article>/is", $content, $matches);
        for ($i = 0; $i < count($matches[1]); $i++) {
            $html = $matches[1][$i];
            $info = array();
            preg_match("/<div class=\"publication-type\" >(.+?)<\\/div>/", $html, $match);
            if ($match) $info["ptype"] = $match[1];
            else $info["ptype"] = "";

            //2020年5月27日16:15:34 葛成瑞 添加摘要模式期刊详情获取的情况

            preg_match("/<div class=\"journal-actions dropdown-block\"><button .+?> (.+?) <\\/button>/", $html, $match);
            if ($match) $info["jour"] = $match[1];
            else $info["jour"] = "";

            $jourTemo = "";
            preg_match("/<span class=\"cit\">(.+?)<\\/span>/", $html, $match);

            if ($match) $jourTemo = $jourTemo . $match[1];

            preg_match("/<span class=\"citation-doi\">(.+?)<\\/span>/", $html, $match);
            if ($match) $jourTemo = $jourTemo . $match[1];

            preg_match("/<span class=\"secondary-date\">(.+?)<\\/span>/", $html, $match);
            if ($match) $jourTemo = $jourTemo . $match[1];

            if ($jourTemo != "") {
                $info["jourdetail"] = $jourTemo;
            } else {
                $info["jourdetail"] = "";
            }

            preg_match("/<span class=\"volume-issue-pages\"> (.+?) <\\/span>/", $html, $match);
            if ($match) $info["vip"] = $match[1];
            else $info["vip"] = "";

            preg_match("/<span class=\"publication-date\" >(.+?)<\\/span>/", $html, $match);
            if ($match) $info["year"] = preg_replace("/<.+?>/is", "", $match[1]);
            else $info["year"] = "";
            //2020年6月16日09:52:06 pmid 解析规则变了 原始备份如下
//            preg_match("/<h1 class=\"heading-title\"> <a.+?data-ga-label=\"(.+?)\">(.+?)<\\/a>/", $html, $match);
//            if ($match) {
//                $info["pmid"] = $match[1];
//                $info["title"] = $match[2];
//            } else {
//                $info["pmid"] = "";
//                $info["title"] = "";
//            }

            preg_match("/<h1 class=\"heading-title\"> <a.+?data-ga-label=\"(.+?)\">(.+?)<\\/a>/", $html, $match);
            if ($match) {
//                $info["pmid"] = $match[1];
                $info["title"] = $match[2];
            } else {
//                $info["pmid"] = "";
                $info["title"] = "";
            }

            preg_match("/<strong class=\"current-id\" title=\"PubMed ID\">(.+?)<\\/strong>/", $html, $match);
            if ($match) {
                $info["pmid"] = $match[1];
            } else {
                $info["pmid"] = "";
            }

            //添加撤稿文章在摘要模式 2023年11月10日08:43:11
            preg_match("/<h3 class=\"usa-alert-heading\">Retracted article<\\/h3>/", $html, $match);
            if ($match) $info["Retracted"] = "1";
            else $info["Retracted"] = "0";

            /*
             * 2021年6月5日10:15:22 原始摘要模式作者备份 新版本加入了 编者 需要两个数组合并
             * */
//            preg_match("/<div class=\"authors-list\">(.+?)<\\/div>/", $html, $match);
//            if ($match) {
//                preg_match_all("/<span class=\"authors-list-item \">(.+?)<\\/sup>/", $match[1], $matchess);
//                $aus = array();
//                for ($j = 0; $j < count($matchess[1]); $j++) {
//                    $au = array();
//                    $auhtml = $matchess[1][$j];
            /*                    preg_match("/<a class=\"full-name\" href=.+?erm=(.+?)&amp;cauthor_id.+?data-ga-action=\"author_link\".+?>(.+?)<\\/a><sup .+?ref=\"linksrc=author_aff\"> (.+?) <\\/a>/", $auhtml, $match1);*/
//                    if ($match1) {
//                        $au["nameiso"] = str_replace("+", " ", $match1[1]);
//                        $au["name"] = $match1[2];
//                        $au["num"] = $match1[3];
//                        $aus[$j] = $au;
//                    }
//                }
//                $info["author"] = $aus;
//            } else {
//                $info["author"] = array();
//            }


            /* 作者div */
            preg_match("/<div class=\"authors\">(.+?)<div class=\"authors-list\">(.+?)<\\/div>/", $html, $match);
            if ($match) {
                preg_match_all("/<span class=\"authors-list-item \">(.+?)<\\/sup>/", $match[2], $matchess);
                $aus = array();
                for ($j = 0; $j < count($matchess[1]); $j++) {
                    $au = array();
                    $auhtml = $matchess[1][$j];
                    preg_match("/<a class=\"full-name\" href=.+?erm=(.+?)&amp;cauthor_id.+?data-ga-action=\"author_link\".+?>(.+?)<\\/a><sup .+?ref=\"linksrc=author_aff\"> (.+?) <\\/a>/", $auhtml, $match1);
                    if ($match1) {
                        $au["nameiso"] = str_replace("+", " ", $match1[1]);
                        $au["name"] = $match1[2];
                        $au["num"] = $match1[3];
                        $aus[$j] = $au;
                    }
                }
                $info["author"] = $aus;
            } else {
                $info["author"] = array();
            }

            /* 编者div */
            preg_match("/<div class=\"editors\">(.+?)<div class=\"authors-list\">(.+?)<span class=\"ending\"/", $html, $match);
            if ($match) {
                preg_match_all("/<span class=\"authors-list-item \">(.+?)<\\/span>/", $match[0], $matchess);
                $aus = array();
                for ($j = 0; $j < count($matchess[0]); $j++) {
                    $au = array();
                    $auhtml = $matchess[0][$j];
                    $auhtml1 = str_replace("&nbsp;", "", str_replace(",&nbsp;", "", preg_replace("/<[^>]+>/is", "", $auhtml)));
                    $au["nameiso"] = $auhtml1;
                    $au["name"] = $auhtml1;
                    $aus[$j] = $au;
                }
                $info["authoreditor"] = $aus;
            } else {
                $info["authoreditor"] = array();
            }


            preg_match_all("/<li data-affiliation-id=\"search-result.+?>(.+?)<\\/li>/", $html, $match);
            if ($match) {
                $affs = array();
                for ($j = 0; $j < count($match[1]) / 2; $j++) {
                    $affhtml = $match[1][$j];
                    preg_match("/<sup class=\"key\">(.+?)<\\/sup> (.+)/", $affhtml, $match1);
                    if ($match1) {
                        $af["num"] = $match1[1];
                        $af["name"] = $match1[2];
                        $affs[$j] = $af;
                    }
                }
                $info["affs"] = $affs;
            } else {
                $info["affs"] = array();
            }


            preg_match("/<span class=\"id-label\"> PMCID: <\\/span> <a .+?> (.+?) <\\/a>/", $html, $match);
            if ($match) $info["pmcid"] = $match[1];
            else $info["pmcid"] = "";

            preg_match("/<span class=\"id-label\"> DOI: <\\/span> <a .+?> (.+?) <\\/a>/", $html, $match);
            if ($match) $info["doi"] = $match[1];
            else $info["doi"] = "";

            //原始备份
//            preg_match("/-en-abstract\"> (.+?) <\\/div>/", $html, $match);
//            if ($match) {
//                $info["abstr"] = $match[1];
            /*                $info["intro"] = preg_replace("/<.+?>/is", "", $match[1]) . "...";*/
//            } else {
//                $info["abstr"] = "";
//                $info["intro"] = "";
//            }


            //2020年7月16日11:06:53 葛成瑞修改摘要模式下的abstract 和intro 精简版的取得是intro  摘要取得的abstract
            preg_match("/eng-abstract\"> (.+?) <\\/div>/", $html, $match);
            if ($match) {
                $info["abstr"] = $match[1];
                $info["intro"] = preg_replace("/<.+?>/is", "", $match[1]) . "...";
            } else {
                $info["abstr"] = "";
                $info["intro"] = "";
            }


            preg_match("/<p> <strong class=\"sub-title\"> Keywords: <\\/strong>.+?<\\/p>/", $html, $match);
            if ($match) $info["keyword"] = $match[0];
            else $info["keyword"] = "";

            preg_match("/-copyright\"> (.+?) <\\/p>/", $html, $match);
            if ($match) $info["copyright"] = $match[1];
            else $info["copyright"] = "";


            //Publication types  MeSH terms  Substances  Associated data  Grant support

            //Publication types
            preg_match("/<h2 class=\"title\"> Publication types <\\/h2>(.+?)<\\/ul> <\\/div>/", $html, $matchpt);
            if ($matchpt) {
                $pts = array();
                preg_match_all("/<li>(.+?)<\\/div><\\/li>/", $matchpt[1], $pubsHtml);
                if ($pubsHtml) {
                    for ($j = 0; $j < count($pubsHtml[1]); $j++) {
                        $pt = array();
                        preg_match("/<button class=\"keyword-actions-trigger trigger keyword-link.+?>(.+?)<\\/button>.+?<ul class=\"keyword-actions-links\">(.+?)<\\/ul>/", $pubsHtml[1][$j], $ptss1);
                        if ($ptss1) {
                            $pt["type"] = 2;
                            $pt["title"] = trim($ptss1[1]);
                            preg_match_all("/<a .+? href=\"(.+?)\".+?>(.+?)<\\/a>/", $ptss1[2], $ptss2);
                            if ($ptss2) {
                                for ($m = 0; $m < count($ptss2[1]); $m++) {
                                    $pt["hrefs"][$m]["name"] = trim($ptss2[2][$m]);
                                    $pt["hrefs"][$m]["href"] = $ptss2[1][$m];
                                }
                            }
                            preg_match("/<a class=\"add-to-search-link .+? data-search-term=\"(.+?)\" .+?> (.+?) <\\/a>/", $ptss1[2], $ptss3);
                            if ($ptss3) {
                                $pt["hrefs"][count($ptss2[1])]["name"] = $ptss3[2];
                                $pt["hrefs"][count($ptss2[1])]["href"] = str_replace("&quot;", "", $ptss3[1]);
                            }
                        }
                        $pts[$j] = $pt;
                    }
                }
                $info["pts"] = $pts;
            } else {
                $info["pts"] = array();
            }

            //MeSH terms
            preg_match("/<h2 class=\"title\"> MeSH terms <\\/h2>(.+?)<\\/ul> <\\/div>/", $html, $matchMesh);
            if ($matchMesh) {
                $kws = array();
                preg_match_all("/<li>(.+?)<\\/div><\\/li>/", $matchMesh[1], $meshHtml);
                if ($meshHtml) {
                    for ($j = 0; $j < count($meshHtml[1]); $j++) {
                        $kw = array();
                        preg_match("/<button class=\"keyword-actions-trigger trigger keyword-link.+?>(.+?)<\\/button>.+?<ul class=\"keyword-actions-links\">(.+?)<\\/ul>/", $meshHtml[1][$j], $kwss1);
                        if ($kwss1) {
                            $kw["type"] = 2;
                            if (strpos($kwss1[1], "asterisk")) {
                                preg_match("/(.+?) <span class=\"asterisk\">/", $kwss1[1], $matchkw);
                                if ($matchkw) {
                                    $kw["title"] = trim($matchkw[1]) . "*";
                                }
                            } else {
                                $kw["title"] = trim($kwss1[1]);
                            }

                            preg_match_all("/<a .+? href=\"(.+?)\".+?>(.+?)<\\/a>/", $kwss1[2], $kwss2);
                            if ($kwss2) {
                                for ($m = 0; $m < count($kwss2[1]); $m++) {
                                    $kw["hrefs"][$m]["name"] = trim($kwss2[2][$m]);
                                    $kw["hrefs"][$m]["href"] = $kwss2[1][$m];
                                }
                            }
                            preg_match("/<a class=\"add-to-search-link .+? data-search-term=\"(.+?)\" .+?> (.+?) <\\/a>/", $kwss1[2], $kwss3);
                            if ($kwss3) {
                                $kw["hrefs"][count($kwss2[1])]["name"] = $kwss3[2];
                                $kw["hrefs"][count($kwss2[1])]["href"] = str_replace("&quot;", "", $kwss3[1]);
                            }
                        }
                        $kws[$j] = $kw;
                    }
                }
                $info["meshs"] = $kws;
            } else {
                $info["meshs"] = array();
            }
            //Substances
            preg_match("/<h2 class=\"title\"> Substances <\\/h2>(.+?)<\\/ul> <\\/div>/", $html, $meshSub);
            if ($meshSub) {
                $subs = array();
                preg_match_all("/<li>(.+?)<\\/div><\\/li>/", $meshSub[1], $subHtml);
                if ($subHtml) {
                    for ($j = 0; $j < count($subHtml[1]); $j++) {
                        $sub = array();
                        preg_match("/<button class=\"keyword-actions-trigger trigger keyword-link.+?>(.+?)<\\/button>.+?<ul class=\"keyword-actions-links\">(.+?)<\\/ul>/", $subHtml[1][$j], $subss1);
                        if ($subss1) {
                            $sub["type"] = 2;
                            $sub["title"] = trim($subss1[1]);
                            preg_match_all("/<a .+? href=\"(.+?)\".+?>(.+?)<\\/a>/", $subss1[2], $subss2);
                            if ($subss2) {
                                for ($m = 0; $m < count($subss2[1]); $m++) {
                                    $sub["hrefs"][$m]["name"] = trim($subss2[2][$m]);
                                    $sub["hrefs"][$m]["href"] = $subss2[1][$m];
                                }
                            }
                            preg_match("/<a class=\"add-to-search-link .+? data-search-term=\"(.+?)\" .+?> (.+?) <\\/a>/", $subss1[2], $subss3);
                            if ($subss3) {
                                $sub["hrefs"][count($subss2[1])]["name"] = $subss3[2];
                                $sub["hrefs"][count($subss2[1])]["href"] = str_replace("&quot;", "", $subss3[1]);
                            }
                        }
                        $subs[$j] = $sub;
                    }
                }
                $info["subs"] = $subs;
            } else {
                $info["subs"] = array();
            }

            //Associated data
            preg_match("/<h3 class=\"title\"> Associated data <\\/h3>(.+?)<\\/ul> <\\/div>/", $html, $associated);
            if ($associated) {
                $assData = array();
                if (strpos($associated[1], "<div class=\"title\">Actions</div>")) {
                    preg_match_all("/<li>(.+?)<\\/div><\\/li>/", $associated[1], $assDatas);
                    if ($assDatas) {
                        for ($j = 0; $j < count($assDatas[1]); $j++) {
                            $assd = array();
                            preg_match("/aria-describedby=\"supplemental-data.+?>(.+?)<\\/button>.+?<ul class=\"supplemental-data-actions-links\">(.+?)<\\/ul>/", $assDatas[1][$j], $ass1);
                            if ($ass1) {
                                $assd["type"] = 2;
                                $assd["title"] = trim($ass1[1]);
                                preg_match_all("/<a .+? title=\"(.+?)\" href=\"(.+?)\".+?>/", $ass1[2], $ass2);
                                if ($ass2) {
                                    for ($m = 0; $m < count($ass2[1]); $m++) {
                                        $assd["hrefs"][$m]["name"] = $ass2[1][$m];
                                        $assd["hrefs"][$m]["href"] = $ass2[2][$m];
                                    }
                                }
                            }
                            $assData[$j] = $assd;
                        }
                    }
                } else {
                    preg_match_all("/<a .+? href=\"(.+?)\" .+?>(.+?)<\\/a>/", $associated[1], $assDatas2);
                    if ($assDatas2) {
                        for ($n = 0; $n < count($assDatas2[1]); $n++) {
                            $assData[$n]["type"] = 1;
                            $assData[$n]["title"] = trim($assDatas2[2][$n]);
                            $assData[$n]["href"] = $assDatas2[1][$n];
                        }
                    }
                }
                $info["assData"] = $assData;
            } else {
                $info["assData"] = array();
            }


            //Grant support

            preg_match("/<h3 class=\"title\"> Grant support <\\/h3>(.+?)<\\/ul><\\/div>/", $html, $supports);
            if ($supports) {
                $grants = array();
                if (strpos($supports[1], "<div class=\"title\">Actions</div>")) {
                } else {
                    preg_match_all("/<a title=\"All articles for grant .+? href=\"(.+?)\" .+?> (.+?) <\\/a>/", $supports[1], $support);
                    if ($support) {
                        for ($n = 0; $n < count($support[1]); $n++) {
                            $grants[$n]["type"] = 1;
                            $grants[$n]["title"] = trim($support[2][$n]);
                            $grants[$n]["href"] = $support[1][$n];
                        }
                    }
                }
                $info["grants"] = $grants;
            } else {
                $info["grants"] = array();
            }

            //Grant support
            preg_match("/<div class=\"funding-statement\">(.+?)<\\/div>/", $html, $funding);
            if ($funding) {
                $grants = array();
                $grants[0]["type"] = 0;
                $grants[0]["title"] = $funding[1];
                $result["grants"] = $grants;
            }

            //full-text-links-list
            preg_match("/<div class=\"full-text-links-list\">(.+?)<\\/div>/is", $html, $matchfull);
            if ($matchfull) {
                $fulltext = array();
                preg_match_all("/<a class=\"link-item(.+?)<\\/a>/", $matchfull[1], $fulls);
                if ($fulls) {
                    for ($j = 0; $j < count($fulls[0]); $j++) {
                        $fullt = array();
                        preg_match("/<a class=\"link-item.+?href=\"(.+?)\"/", $fulls[0][$j], $full1);
                        if ($full1) {
                            $fullt["href"] = $full1[1];
                        } else {
                            $fullt["href"] = "";
                        }
                        preg_match("/<img src=\"(.+?)\"/", $fulls[0][$j], $full2);
                        if ($full2) {
                            $fullt["img"] = $full2[1];
                        } else {
                            $fullt["img"] = "";
                        }
                        preg_match("/<span class=\"text\"> (.+?) <\\/span>/", $fulls[0][$j], $full3);
                        if ($full3) {
                            $fullt["name"] = $full3[1];
                            if ($fullt["name"] == "Free PMC article") {
                                $fullt["img"] = "../img/pmcImg.png";
                            }
                        } else {
                            $fullt["name"] = "";
                        }
                        $fulltext[$j] = $fullt;
                    }
                }
                $info["fulltext"] = $fulltext;
            }
            $info["source"] = $source . " " . $info["jour"] . $info["time"] . " - " . "<em>" . $info["ptype"] . "</em>";

            $list[$i] = $info;
        }

    } else {
        //2020年7月17日09:09:14 葛成瑞 列表页
        /*        preg_match_all("/<article class=\"labs-full-docsum\".+?>(.+?)<\\/article>/is", $content, $matches);*/ //原始备份

        //2022年2月18日12:46:55 因为部分会有官方的提示 需要把第一个去掉 第一个只有标题
        preg_match("/<section class=\"search-results-list\">(.+?)<\\/section>/", $content, $match);
        // 下面匹配规则是匹配最准确那个 为了官网写的规则
        preg_match("/<section class=\"matching-citations search-results-list\">(.+?)<\\/section>/", $content, $match2);
        if ($match) {
            preg_match_all("/<article class=\"full-docsum\".+?>(.+?)<\\/article>/is", $match[1], $matches);
            for ($i = 0; $i < count($matches[1]); $i++) {
                $html = $matches[1][$i];
                $info = array();
                /*            preg_match("/<a class=\"labs-docsum-title\".+?>(.+?)<\\/a>/", $html, $match);*/ //备份
                preg_match("/<a class=\"docsum-title\".+?>(.+?)<\\/a>/", $html, $match);
                if ($match) {
                    $info["title"] = $match[1];
                } else {
                    $info["title"] = "";
                }
                preg_match("/<div class=\"full-view-snippet\">(.+?)<\\/div>/is", $html, $match);
                if ($match) {
                    $info["intro"] = $match[1];
                } else {
                    $info["intro"] = "";
                }
                //2020年5月27日14:54:26 gcr 修改source 和作者分开显示
//            原始备份
                /*            preg_match("/<div class=\"labs-docsum-citation.+?>(.+?)PMID/", $html, $match);*/
//            if ($match) {
                /*                $info["source"] = preg_replace("/<span.+?>/", "", str_replace("</span>", "", $match[1]));*/
//            } else {
//                $info["source"] = "";
//            }
//            preg_match("/<span class=\"labs-docsum-journal-citation full-journal-citation\">(.+?)<\\/span>/", $html, $match);
                preg_match("/<span class=\"docsum-journal-citation full-journal-citation\">(.+?)<\\/span>/", $html, $match);
                if ($match) {
                    $info["source"] = $match[1];
                } else {
                    $info["source"] = "";
                }

                // author
//            preg_match("/<span class=\"labs-docsum-authors full-authors\">(.+?)<\\/span>/",$html,$match); //备份
                preg_match("/<span class=\"docsum-authors full-authors\">(.+?)<\\/span>/", $html, $match);
                if ($match) {
                    $info["authorls"] = $match[1];
                } else {
                    $info["authorls"] = "";
                }

                //原始备份
//            preg_match("/data-ga-label=\"(.+?)\"/", $html, $match);
//            if ($match) {
//                $info["pmid"] = intval(str_replace("</b>", "", str_replace("<b>", "", $match[1])));
//            } else {
//                $info["pmid"] = 0;
//            }
                preg_match("/<span class=\"docsum-pmid\">(.+?)<\\/span>/", $html, $match);
                if ($match) {
                    $info["pmid"] = $match[1];
                } else {
                    $info["pmid"] = 0;
                }
                //添加撤稿分析 2023年11月9日17:28:00
                preg_match("/<span class=\"correction-label spaced-citation-item citation-part\">(.+?)<\\/span>/", $html, $match);
                if ($match) {
                    $info["Retracted"] = "1";
                } else {
                    $info["Retracted"] = "0";
                }
                $list[$i] = $info;
            }
        }
        if ($match2) {
            preg_match_all("/<article class=\"full-docsum\".+?>(.+?)<\\/article>/is", $match2[1], $matches);
            for ($i = 0; $i < count($matches[1]); $i++) {
                $html = $matches[1][$i];
                $info = array();
                /*            preg_match("/<a class=\"labs-docsum-title\".+?>(.+?)<\\/a>/", $html, $match);*/ //备份
                preg_match("/<a class=\"docsum-title\".+?>(.+?)<\\/a>/", $html, $match);
                if ($match) {
                    $info["title"] = $match[1];
                } else {
                    $info["title"] = "";
                }
                preg_match("/<div class=\"full-view-snippet\">(.+?)<\\/div>/is", $html, $match);
                if ($match) {
                    $info["intro"] = $match[1];
                } else {
                    $info["intro"] = "";
                }
                //需要替换准确匹配的简短来源 2023年5月9日13:46:00
                preg_match("/<span class=\"docsum-journal-citation short-journal-citation\">(.+?)<\\/span>/", $html, $match);
                if ($match) {
                    $info["source"] = $match[1];
                } else {
                    $info["source"] = "";
                }
                // author
                //需要替换准确匹配的简短作者 2023年5月9日13:46:34
                preg_match("/<span class=\"docsum-authors short-authors\">(.+?)<\\/span>/", $html, $match);
                if ($match) {
                    $info["authorls"] = $match[1];
                } else {
                    $info["authorls"] = "";
                }
                preg_match("/<span class=\"docsum-pmid\">(.+?)<\\/span>/", $html, $match);
                if ($match) {
                    $info["pmid"] = $match[1];
                } else {
                    $info["pmid"] = 0;
                }
                $list2[$i] = $info;
            }
        }
        //两个集合放在一起
        $list = array_merge($list2, $list);
    }
    $result["list"] = $list;
} else {
    $result["type"] = 0;
}

$etime = microtime(true);
//$result["ipnum"]=$num;
$result["ip"] = $ip;
$result["cost"] = $etime - $stime;
echo json_encode($result);


function getHtml($term, $filter, $sort,$sort_order, $page, $format, $size, $ip, $useragent, $cookie)
{
    $term = stripslashes($term);
    $term = urlencode($term);
    $term = str_replace("%5C%27","%27",$term);
    $term = str_replace("%5C%%22","%22",$term);
    $term = str_replace("%5C%5C","%5C",$term);
    $cookie = "";
//     $url = "https://pubmed.ncbi.nlm.nih.gov/?term=" . $term);
    $url = "https://pubmed.ncbi.nlm.nih.gov/?term=" . $term;
    if (!empty($filter)) {
        foreach (explode('ss,', $filter) as $value) {
            $url = $url . "&filter=" . $value;
        }
    }

    if (!empty($sort)) {
        $url = $url . "&sort=" . $sort;
    }

    if (!empty($sort_order)){
        $url = $url . "&sort_order=" . $sort_order;
    }

    if (!empty($page)) {
        $url = $url . "&page=" . $page;
    }

    if (!empty($format)) {
        $url = $url . "&format=" . $format;
    }
    if (!empty($size)) {
        $url = $url . "&size=" . $size;
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_REFERER, "https://pubmed.ncbi.nlm.nih.gov/");
    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    if (!empty($ip)) {
        curl_setopt($ch, CURLOPT_INTERFACE, $ip);
    }
    if (!empty($cookie)) {
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    }

    $content = curl_exec($ch);
    preg_match("/location:(.+?)\r\n/i", $content, $match);
    if ($match) {
        curl_setopt($ch, CURLOPT_URL, "https://pubmed.ncbi.nlm.nih.gov" . trim($match[1]));
        $content = curl_exec($ch);
    }
    curl_close($ch);
    list($header, $body) = explode("\r\n\r\n", $content);
    preg_match_all('/Set-Cookie: (.*);/isU', $header, $matches);
    $cookie = implode(";", $matches[1]);
    return array("body" => $body, "cookie" => $cookie);
}

?>
